package com.example.demo.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.Map;

public class WordExportUtil {


    private static WordExportUtil service = null;

    private WordExportUtil() {
        super();
    }

    public static WordExportUtil getInstance() {
        if (service == null) {
            synchronized (WordExportUtil.class) {
                if (service == null) {
                    service = new WordExportUtil();
                }
            }
        }
        return service;
    }


    /**
     * @param templateFilePath eg: /template/test/test.ftl
     * @param dataMap
     * @param exportFilePath   eg: /tmp/test/test123.doc
     * @param loadType         设置路径加载方式。1-绝对路径，2-项目相对路径
     * @return
     * @throws Exception
     */
    public File createDocFile(String templateFilePath, Map<String, Object> dataMap, String exportFilePath, int loadType) throws Exception {
        Template t = null;
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        configuration.setDefaultEncoding("UTF-8");
        try {
            templateFilePath = pathReplace(templateFilePath);
            String ftlPath = templateFilePath.substring(0, templateFilePath.lastIndexOf("/"));
            if (loadType == 1) {
                configuration.setDirectoryForTemplateLoading(new File(ftlPath)); // FTL文件所存在的位置
            } else {
                configuration.setClassForTemplateLoading(this.getClass(), ftlPath);//以类加载的方式查找模版文件路径
            }


            String ftlFile = templateFilePath.substring(templateFilePath.lastIndexOf("/") + 1);
            t = configuration.getTemplate(ftlFile); // 模板文件名

            File outFile = new File(exportFilePath);
            Writer out = null;

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));

            t.process(dataMap, out);

        } catch (Exception e) {

            throw e;
        }

        return null;
    }

    /**
     * 把路径的\替换成/
     *
     * @param path
     * @return
     */
    private String pathReplace(String path) {
        while (path != null && path.contains("\\")) {
            path = path.replace("\\", "/");
        }
        return path;
    }

    /**
     * 测试用的
     *
     * @param dataMap
     */
    public static void getData(Map<String, Object> dataMap) {
        dataMap.put("developer", "1");
        dataMap.put("contact", "1");
        dataMap.put("phoneNum", "1");
        dataMap.put("proName", "1");
        dataMap.put("contractBdgetPrice", "1");
        dataMap.put("proTotalPrice", "1");
        dataMap.put("buildType", "1");
        dataMap.put("estimateTime", "1");
        dataMap.put("contractWay", "1");
        dataMap.put("orgType", "1");
        dataMap.put("proxyName", "1");
        dataMap.put("proxyPeople", "1");
        dataMap.put("proxyTel", "1");
        dataMap.put("contractContain", "1");
        dataMap.put("contractChoseReason", "1");
    }
}
